package com.storlead.email.controller;

import com.storlead.email.service.EmailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;

/**
 * @Author: Timo
 * @Date: 2019/2/11 11:34
 * @Description: 类用途：
 */
@Slf4j
@RestController
@RequestMapping(("/email"))
public class EmailController {

//    @Qualifier("javaMailSenderGmail")
//    @Autowired
//    private JavaMailSender gmailSender;

    @Qualifier("javaMailSenderQQ")
    @Autowired
    private JavaMailSender qqSender;

    @Autowired
    private EmailService emailService;

    private String toEmail = "15662174827@163.com";

    @GetMapping("/send_163")
    public String send163Email() {
        return "OK";
    }


    @GetMapping("/send_qq")
    public String sendQQEmail() throws MessagingException {
        emailService.sendEmail(toEmail, "简单测试一下", "ssssssssssss");
        return "OK";
    }

    @GetMapping("/send_html_qq")
    public String sendQQHtmlEmail() throws MessagingException {
        emailService.sendHtmlEmail(toEmail, "简单测试一下", "ssssssssssss", null);
        return "OK";
    }

    @GetMapping("receiv_email")
    public String receivEmail(String emailType) {
        emailService.receivEmail(emailType);
        return "";
    }


    @GetMapping("/send_test_gmail")
    public String sendTest() {
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setTo(toEmail);
            message.setSubject("主题-邮件主题");
            message.setText("邮件内容");
//            gmailSender.send(message);
            return "send is ok ";
        } catch (Exception e) {
            return "发送失败" + e.toString() + e.getMessage();
        }
    }

    /**
     * spring.mail.host=smtp.qq.com
     * spring.mail.username=用户名
     * spring.mail.password=密码
     * spring.mail.properties.mail.smtp.auth=true
     * spring.mail.properties.mail.smtp.starttls.enable=true
     * spring.mail.properties.mail.smtp.starttls.required=true
     *
     * @return
     */
    @GetMapping("/send_test_qq")
    public String sendTestQQ() throws MessagingException {
        try {

            JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
            mailSender.setHost("smtp.qq.com");
            mailSender.setUsername("1002512666@qq.com");
            mailSender.setPassword("gshjwpkyfwkgbddg");
            Properties props = mailSender.getJavaMailProperties();
            props.put("mail.transport.protocol", "smtp");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.starttls.required", "true");


            MimeMessage message = mailSender.createMimeMessage();
            message.getMessageID();

            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom("1002512666@qq.com");
            helper.setTo(toEmail);
            helper.setSubject("主题-测试主题");
            helper.setText("邮件内容", false);
            helper.setSentDate(new Date());

            mailSender.send(message);
            return "qq send is ok ";
        } catch (Exception e) {
            return "发送失败" + e.toString() + e.getMessage();
        }
    }



    /**
     * spring.mail.host=smtp.qq.com
     * spring.mail.username=用户名
     * spring.mail.password=密码
     * spring.mail.properties.mail.smtp.auth=true
     * spring.mail.properties.mail.smtp.starttls.enable=true
     * spring.mail.properties.mail.smtp.starttls.required=true
     *
     * @return
     */
    @GetMapping("/send_test_163")
    public String sendTest163() throws MessagingException {
        try {

            JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
            mailSender.setHost("smtp.163.com");
            mailSender.setUsername("15662174827@163.com");
            mailSender.setPassword("jxy19910320");
            Properties props = mailSender.getJavaMailProperties();
            props.put("mail.transport.protocol", "smtp");
            props.put("mail.default-encoding", "UTF-8");
//
//            props.put("mail.smtp.auth", "true");
//            props.put("mail.smtp.starttls.enable", "true");
//            props.put("mail.smtp.starttls.required", "true");


            MimeMessage message = mailSender.createMimeMessage();
            message.getMessageID();

            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom("15662174827@163.com");
            helper.setTo("1002512666@qq.com");
            helper.setSubject("主题-测试主题");
            helper.setText("邮件内容", false);
            helper.setSentDate(new Date());

            mailSender.send(message);
            return "163 send is ok ";
        } catch (Exception e) {
            return "发送失败" + e.toString() + e.getMessage();
        }
    }

}
